package com.biaobiaoqi.scoreanalysis.serviceImpl;

import java.util.List;

import com.biaobiaoqi.scoreanalysis.exception.DuplicateSchoolYearException;
import com.biaobiaoqi.scoreanalysis.hibernate.bean.SchoolYear;
import com.biaobiaoqi.scoreanalysis.service.ISchoolyearService;
import com.biaobiaoqi.scoreanalysis.util.Semester;

public class SchoolYearServiceImpl extends DaoServiceImpl<SchoolYear> implements
		ISchoolyearService {

	@Override
	public void addSchoolYear(int begin, int end, Semester semester)
			throws DuplicateSchoolYearException {
		if (findSchoolYear(begin, end, semester) != null) {
			throw new DuplicateSchoolYearException();
		} else {
			SchoolYear schoolYear = new SchoolYear();
			schoolYear.setBeginYear(begin);
			schoolYear.setEndYear(end);
			schoolYear.setSemester(semester);
			getDao().create(schoolYear);
		}
	}

	@Override
	public SchoolYear findSchoolYear(int schoolYearId) {
		return getDao().findById(SchoolYear.class, schoolYearId);
	}

	@Override
	public SchoolYear findSchoolYear(int begin, int end, Semester semester) {
		return getDao()
				.queryUnique(
						"from SchoolYear s where s.beginYear=? and s.endYear=? and s.semester=?",
						begin, end, semester);
	}

	@Override
	public List<SchoolYear> listSchoolYear(int begin, int number) {
		return getDao().queryList(
				"from SchoolYear s order by s.beginYear desc", begin, number);
	}

	@Override
	public void deleteSchoolYear(int id) {
		getDao().delete(findSchoolYear(id));
	}

}
